#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include "/home/lhr/http-server/dbManager.hpp"
#include "/home/lhr/http-server/Util.hpp"

Json::Value result;
DBManager &db = DBManager::GetInstance();

void processPOST()
{
    std::string reqParam;
    Util::GetReqParam(reqParam);

    std::string str1;
    std::string str2;
    Util::CutString(reqParam, str1, str2, "&");

    std::string Title;
    std::string title;
    Util::CutString(str1, Title, title, "=");
    title = Util::UrlDecode(title);

    std::string Content;
    std::string content;
    Util::CutString(str2, Content, content, "=");
    content = Util::UrlDecode(content);

    // 获取token环境变量
    std::string session_token = getenv("SESSION_TOKEN");
    // 根据session_token获取用户ID
    int userId = 0;
    int ret = db.GetUserIdByToken(session_token, userId);
    if (ret != SUCCESS)
    {
        result["success"] = false;
        result["message"] = "获取用户ID失败";
        return;
    }
    // 调用数据库接口插入文章
    ret = db.addArticle(title, content, userId);
    if (ret != SUCCESS)
    {
        result["success"] = false;
        result["message"] = "文章发布失败";
        return;
    }

    result["success"] = true;
    result["message"] = "文章发布成功";
}

void processGET()
{
    std::string reqParam;
    Util::GetReqParam(reqParam);

    // 处理查看文章的请求
    std::string article_ID;
    std::string article_id;
    Util::CutString(reqParam, article_ID, article_id, "=");

    int articleID = atoi(article_id.c_str());
    LOG(INFO, "article_id: %d", articleID);

    // 获取文章
    Json::Value article = db.getArticleById(article_id);
    if (article.isNull())
    {
        result["success"] = false;
        result["message"] = "未找到指定的文章";
        return;
    }
    result["success"] = true;
    result["title"] = article["title"];
    result["author"] = article["author"];
    result["date"] = article["date"];
    result["content"] = article["content"];

    // 获取评论
    Json::Value comments(Json::arrayValue);
    Json::Value articleComments = db.getCommentsForArticle(article_id);
    for (const auto &comment : articleComments)
    {
        Json::Value commentJson;
        commentJson["content"] = comment["content"];
        commentJson["commenter"] = comment["commenter"];
        commentJson["date"] = comment["date"];
        comments.append(commentJson);
    }
    result["comments"] = comments;
}

int main()
{
    // 获取请求方法
    std::string reqMethod = getenv("METHOD");
    if (reqMethod == "POST")
    {
        processPOST();
    }
    else if (reqMethod == "GET")
    {
        processGET();
    }
    // 其他请求方法
    else
    {
        result["success"] = false;
        result["message"] = "未定义的请求方法";
    }

    std::string http_resBody = result.toStyledString();
    // 向管道输出cgi请求的http响应
    std::string http_res;
    http_res += "HTTP/1.0 200 OK\r\n";
    http_res += "Content-Type: application/json\r\n";
    http_res += "Content-Length: ";
    http_res += std::to_string(http_resBody.length());
    http_res += "\r\n";
    http_res += "\r\n";
    http_res += http_resBody;

    std::cout << http_res;
    return 0;
}
